package com.zle.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 日期方法公共类
 * 
 * @author zhanglei
 *
 *         2015年05月2日 上午11:31:39
 */
public class DateUtil {
	private static final Log logger = LogFactory.getLog(DateUtil.class);

	public static void main(String[] args) {
		// System.out.println("20150605".substring(6,8));
		// List ss = getTableSuffixByDate(1433692800,1433751760);
		// List ss2 =
		// getTableSuffixByDate(1433751760,getDateAfterDay(1433751760,1));
		// List ss3 =
		// getTableSuffixByDate(getDateAfterDay(1433751760,1),getDateAfterDay(getDateAfterDay(1433751760,1),1));
		// System.out.println(ss3);
		// System.out.println(getTimesMonthnight(1, 1446468098));
		System.out.println(switchYyyymmddToWeekFormat("20150108"));

	}

	/**
	 * 判断某天是周几
	 * 
	 * @param time
	 *            日期字符串(秒数)
	 * @return 周的第几天
	 */
	public static Integer getDayOfWeek(int time) {
		Date date = new Date(time * 1000l);
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		Integer week = cal.get(Calendar.DAY_OF_WEEK);
		return week;
	}

	/**
	 * 判断某天是周几
	 * 
	 * @param time
	 *            日期字符串(YYYYMMdd)
	 * @return 周的第几天
	 */
	public static Integer getDayOfWeek(String time) {
		if (time == null) {
			return null;
		}
		SimpleDateFormat inPutformatter = new SimpleDateFormat("yyyyMMdd");
		try {
			Date date = inPutformatter.parse(time);
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			Integer week = cal.get(Calendar.DAY_OF_WEEK);
			return week;
		} catch (ParseException e) {
			logger.error(e);
		}
		return null;
	}

	/**
	 * 转换日期格式：int秒 转为 YYYYMMDD
	 * 
	 * @Describe: <br>
	 * @return YYYYMMDD
	 */
	public static String switchSecondeToYYYYMMDD(int secondes) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		return formatter.format(new Date(secondes * 1000l));
	}

	/**
	 * 将格式为 YYYYMMdd的日期转为YYYY-MM-DD
	 * 
	 * @param time
	 *            日期字符串(YYYYMMdd)
	 * @return 日期字符串(YYYY-MM-DD)
	 */
	public static String switchYyyymmddToDayFormat(String time) {
		if (time == null) {
			return null;
		}
		SimpleDateFormat inPutformatter = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat outPutFormatter = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return "'" + outPutFormatter.format(inPutformatter.parse(time))
					+ "'";
		} catch (ParseException e) {
			logger.error(e);
		}
		return null;
	}

	/**
	 * 将格式为 YYYYMMdd的日期转为YYYY-MM
	 * 
	 * @param time
	 *            日期字符串(YYYYMMdd)
	 * @return 日期字符串(YYYY-MM)
	 */
	public static String switchYyyymmddToMonthFormat(String time) {
		if (time == null) {
			return null;
		}
		SimpleDateFormat inPutformatter = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat outPutFormatter = new SimpleDateFormat("yyyy-MM");
		try {
			return "'" + outPutFormatter.format(inPutformatter.parse(time))
					+ "'";
		} catch (ParseException e) {
			logger.error(e);
		}
		return null;
	}

	/**
	 * 将格式为 YYYYMMdd的日期转为年第几周如:20150108->201502(2015年第2周)
	 * 
	 * @param time
	 *            日期字符串(YYYYMMdd)
	 * @return 日期字符串(YYYYWW)
	 */
	public static String switchYyyymmddToWeekFormat(String time) {
		if (time == null) {
			return null;
		}
		SimpleDateFormat inPutformatter = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat outPutformatter = new SimpleDateFormat("yyyy");
		try {
			Date date = inPutformatter.parse(time);
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			int week = cal.get(Calendar.WEEK_OF_YEAR);
			return "'" + outPutformatter.format(inPutformatter.parse(time))
					+ (week < 10 ? "0" + week : week) + "'";
		} catch (ParseException e) {
			logger.error(e);
		}
		return null;
	}

	/**
	 * int类型时间转为字符串时间 yyyy-MM-dd HH:mm:ss
	 * 
	 * @param time
	 *            整数类型(秒)
	 * @return 字符串: yyyy-MM-dd HH:mm:ss
	 */
	public static String getStringDate(Integer time) {
		if (time == null) {
			return null;
		}
		Date date = new Date(time * 1000l);
		SimpleDateFormat dateformat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		return dateformat.format(date);
	}

	/**
	 * int类型时间转为字符串时间HH:mm:ss
	 * 
	 * @param time
	 *            整数类型(秒)
	 * @return 字符串: HH:mm:ss
	 */
	public static String getStringDateHMS(Integer time) {
		if (time == null) {
			return null;
		}
		Date date = new Date(time * 1000l);
		SimpleDateFormat dateformat = new SimpleDateFormat("HH:mm:ss");
		return dateformat.format(date);
	}

	/**
	 * 获取当前时间，格式：YYYYMMDD
	 * 
	 * @Describe: <br>
	 * @return YYYYMMDD
	 */
	public static String getCurrentDate() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		return formatter.format(new Date());
	}

	/**
	 * 字符串转为int类型时间 (秒)
	 * 
	 * @param String
	 *            : yyyy-MM-dd HH:mm:ss
	 * @return int
	 */
	public static int getIntDate(String time) {

		SimpleDateFormat dateformat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		int date = 0;
		try {
			date = Integer.parseInt(String.valueOf(dateformat.parse(time)
					.getTime() / 1000l));
		} catch (ParseException e) {
			logger.error(e);
		}
		return date;
	}

	/**
	 * 获取日志分布的表的后缀集合
	 * 
	 * @param beginTime
	 *            开始时间(秒)
	 * @param endTime
	 *            结束时间(秒)
	 * @return List<String>:{"20150420","20150421"}
	 */
	public static List<String> getTableSuffixByDate(int beginTime, int endTime) {
		List<String> dates = new ArrayList<String>();
		try {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
			Date beginDate = new Date(getDayOfBegin(beginTime) * 1000l);
			Date endDate = new Date(endTime * 1000l);
			while (beginDate.getTime() < endDate.getTime()) {
				dates.add(formatter.format(beginDate));
				beginDate = new Date(getDateAfterDay(beginDate.getTime(), 1));
			}
		} catch (Exception e) {
			logger.error(e);
		}
		return dates;
	}

	/**
	 * 获取系统当前时间
	 * 
	 * @return int (秒)
	 */
	public static int getCurrentSeconds() {
		Date data = new Date();
		return (int) (data.getTime() / 1000l);
	}
	/**
	 * 获取系统当前时间
	 * 
	 * @return String (yyyy-MM-dd HH:mm:ss)
	 */
	public static String getCurrentTime() {
		return getStringDate(getCurrentSeconds());
	}

	/**
	 * 得到后一天的时间
	 * 
	 * @param seconds
	 *            秒
	 * @return int (秒)
	 */
	public static Integer getNextDayTime(Integer seconds) {
		if (seconds == null) {
			return null;
		}
		return getDateAfterDay(seconds, 1);
	}

	/**
	 * 得到下周执行时间
	 * 
	 * @param seconds
	 *            秒
	 * @return int (秒)
	 */
	public static Integer getNextWeekTime(Integer seconds) {
		if (seconds == null) {
			return null;
		}
		return getDateAfterWeek(seconds, 1);
	}

	/**
	 * 得到下个月执行时间
	 * 
	 * @param seconds
	 *            秒
	 * @return
	 */
	public static Integer getNextMonthTime(Integer seconds) {
		if (seconds == null) {
			return null;
		}
		return getDateAfterMonth(seconds, 1);
	}

	/**
	 * 得到几天后的时间
	 * 
	 * @param seconds
	 *            秒
	 * @param day
	 *            天数
	 * @return int 秒
	 */
	public static Integer getDateAfterDay(Integer seconds, Integer day) {
		if (seconds == null || day == null) {
			return null;
		}
		return (int) (getDateAfterDay(seconds * 1000l, day) / 1000l);
	}

	/**
	 * 得到几天后的时间
	 * 
	 * @param milliSeconds
	 *            毫秒
	 * @param day
	 *            天数
	 * @return Long (毫秒)
	 */
	public static Long getDateAfterDay(Long milliSeconds, Integer day) {
		if (milliSeconds == null || day == null) {
			return null;
		}
		Date data = new Date(milliSeconds);
		Calendar now = Calendar.getInstance();
		now.setTime(data);
		now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
		return now.getTimeInMillis();
	}

	/**
	 * 得到某天零点的秒数
	 * 
	 * @param date
	 * @return int (秒)
	 * @throws ParseException
	 */
	public static Integer getDayOfBegin(Integer date) {
		if (date == null) {
			return null;
		}
		SimpleDateFormat adf1 = new SimpleDateFormat("dd/MM/yyyy");
		SimpleDateFormat adf2 = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		Date temp = new Date(date * 1000l);
		try {
			return (int) (adf2.parse(adf1.format(temp).concat(" 00:00:00"))
					.getTime() / 1000l);
		} catch (ParseException e) {
			logger.error(e);
		}
		return 0;
	}

	/**
	 * 得到某天24点的秒数
	 * 
	 * @param date
	 *            秒
	 * @throws ParseException
	 */
	public static Integer getDayOfEnd(Integer date) {
		if (date == null) {
			return null;
		}
		return getDayOfBegin(getDateAfterDay(date, 1)) - 1;
	}

	/**
	 * 得到几周后的时间
	 * 
	 * @param seconds
	 *            秒
	 * @param week
	 * @return int (秒)
	 */
	public static Integer getDateAfterWeek(Integer seconds, Integer week) {
		if (seconds == null || week == null) {
			return null;
		}
		Date data = new Date(seconds * 1000l);
		Calendar now = Calendar.getInstance();
		now.setTime(data);
		now.set(Calendar.WEDNESDAY, now.get(Calendar.WEDNESDAY) + week);
		return (int) (now.getTimeInMillis() / 1000l);
	}

	/**
	 * 得到几月后的时间
	 * 
	 * @param seconds
	 *            秒
	 * @param month
	 * @return int (秒)
	 */
	public static Integer getDateAfterMonth(Integer seconds, Integer month) {
		if (seconds == null || month == null) {
			return null;
		}
		Date data = new Date(seconds * 1000l);
		Calendar now = Calendar.getInstance();
		now.setTime(data);
		now.set(Calendar.MONTH, now.get(Calendar.MONTH) + month);
		return (int) (now.getTimeInMillis() / 1000l);
	}

	/**
	 * 获得当天0点时间
	 * 
	 * @return
	 */
	public static Integer getTimesmorning() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return (int) (cal.getTimeInMillis() / 1000l);
	}

	/**
	 * 获得当天24点时间
	 * 
	 * @return
	 */

	public static Integer getTimesnight() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 24);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return (int) (cal.getTimeInMillis() / 1000l);
	}

	/**
	 * 获得上周一0点时间
	 * 
	 * @return
	 */
	public static Integer getPreviousTimesWeekmorning() {
		Calendar now = Calendar.getInstance();
		now.set(Calendar.DATE, now.get(Calendar.DATE) - 7);

		Calendar cal = Calendar.getInstance();
		cal.setTime(now.getTime());
		cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY),
				cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return (int) (cal.getTimeInMillis() / 1000l);
	}

	private static Date getPreviousTimesWeekmorningDate() {
		Calendar now = Calendar.getInstance();
		now.set(Calendar.DATE, now.get(Calendar.DATE) - 7);

		Calendar cal = Calendar.getInstance();
		cal.setTime(now.getTime());
		cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY),
				cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return cal.getTime();
	}

	/**
	 * 获得某周日24点时间
	 * 
	 * @return
	 */
	public static Integer getPreviousTimesWeeknight(int num) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getPreviousTimesWeekmorningDate());
		cal.add(Calendar.DAY_OF_WEEK, 7 * num);
		return (int) (cal.getTimeInMillis() / 1000l - 1);
	}

	/**
	 * 获得本周一0点时间
	 * 
	 * @return
	 */
	public static Integer getTimesWeekmorning() {
		Calendar cal = Calendar.getInstance();
		cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY),
				cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return (int) (cal.getTimeInMillis() / 1000l);
	}

	private static Date getTimesWeekmorningDate() {
		Calendar cal = Calendar.getInstance();
		cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY),
				cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return cal.getTime();
	}

	/**
	 * 获得某周日24点时间
	 * 
	 * @return
	 */
	public static Integer getTimesWeeknight(int num) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getTimesWeekmorningDate());
		cal.add(Calendar.DAY_OF_WEEK, 7 * num);
		return (int) (cal.getTimeInMillis() / 1000l - 1);
	}

	/**
	 * 获得本月第一天0点时间
	 * 
	 * @return
	 */
	public static Integer getTimesMonthmorning() {
		Calendar cal = Calendar.getInstance();
		cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY),
				cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		cal.set(Calendar.DAY_OF_MONTH,
				cal.getActualMinimum(Calendar.DAY_OF_MONTH));
		return (int) (cal.getTimeInMillis() / 1000l);
	}

	/**
	 * 获得本月第一天0点日期
	 * 
	 * @return
	 */
	private static Date getTimesMonthmorningDate() {
		Calendar cal = Calendar.getInstance();
		cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY),
				cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		cal.set(Calendar.DAY_OF_MONTH,
				cal.getActualMinimum(Calendar.DAY_OF_MONTH));
		return cal.getTime();
	}

	/**
	 * 获得某月第一天0点日期
	 * 
	 * @return
	 */
	private static Date getTimesMonthmorningDate(int time) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date(time * 1000l));
		cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY),
				cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		cal.set(Calendar.DAY_OF_MONTH,
				cal.getActualMinimum(Calendar.DAY_OF_MONTH));
		return cal.getTime();
	}

	/**
	 * 获得某月最后一天24点时间
	 * 
	 * @param num
	 *            num=1为当月,num=2为下个月末...
	 * @return
	 */
	public static Integer getTimesMonthnight(int num) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getTimesMonthmorningDate());
		cal.add(Calendar.MONTH, num - 1);
		cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY),
				cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		cal.set(Calendar.DAY_OF_MONTH,
				cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		cal.set(Calendar.HOUR_OF_DAY, 24);
		return (int) (cal.getTimeInMillis() / 1000l - 1l);
	}

	/**
	 * 获得某月最后一天24点时间
	 * 
	 * @param time
	 *            某天的秒
	 * @param num
	 *            num=1为当月,num=2为下个月末.....
	 * @return
	 */
	public static Integer getTimesMonthnight(int num, int time) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getTimesMonthmorningDate(time));
		cal.add(Calendar.MONTH, num - 1);
		cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY),
				cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		cal.set(Calendar.DAY_OF_MONTH,
				cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		cal.set(Calendar.HOUR_OF_DAY, 24);
		return (int) (cal.getTimeInMillis() / 1000l - 1l);
	}

	/**
	 * 获得几天前/后的时间(负数为前几天)
	 * 
	 * @return
	 */
	public static Integer getTimesBeforeTime(int time, int amoumt) {
		Date dNow = new Date(time * 1000l); // 当前时间
		Calendar cal = Calendar.getInstance(); // 得到日历
		cal.setTime(dNow);// 把当前时间赋给日历
		cal.add(Calendar.DAY_OF_MONTH, amoumt); // 设置为前一天
		return (int) (cal.getTimeInMillis() / 1000l);// 得到前一天的时间
	}

}
